//
//  LoginViewController.swift
//  HiBook
//
//  Created by Apple on 16/8/4.
//  Copyright © 2016年 Apple. All rights reserved.
//

import UIKit
import WebKit
class LoginViewController: UIViewController,WKNavigationDelegate,WKUIDelegate,WKScriptMessageHandler {

    
    var moreWebView : WKWebView?
    
    var hidden : Bool?


    override func viewDidLoad() {
        super.viewDidLoad()
        
        let config = WKWebViewConfiguration()
        
        
        self.moreWebView = WKWebView(frame: CGRectMake(0, 46, SCREEN_WIDTH, SCREEN_HEIGHT) ,configuration: config)
        
        self.moreWebView?.UIDelegate = self
        
        self.moreWebView?.navigationDelegate = self
        
        self.moreWebView?.scrollView.scrollEnabled = false
        
        
        self.moreWebView?.configuration.userContentController.addScriptMessageHandler(self, name: "responesToPhone")
        
//        self.moreWebView?.allowsBackForwardNavigationGestures = true
        
        let request = NSURLRequest(URL: NSURL(string: "http://\(MAIN_IP):8000/web/register.html")!)
        
        self.moreWebView?.loadRequest(request)
        
        self.view.addSubview(moreWebView!)
        
        let navbar = UINavigationBar(frame: CGRectMake(0,0,SCREEN_WIDTH,55))
        navbar.barTintColor = UIColor.whiteColor()
        navbar.tintColor = UIColor.blackColor()
        
        let navtitle = UINavigationItem(title: "登陆")
        navtitle.titleView?.tintColor = MAIN_COLOR
        navbar.pushNavigationItem(navtitle, animated: false)
        
        //        UINavigationBar.setItems([navbar])
        
        self.view.addSubview(navbar)
//        
//        let dateFrom = NSDate(timeIntervalSince1970: 0)
//        WKWebsiteDataStore.defaultDataStore().removeDataOfTypes([WKWebsiteDataTypeCookies,WKWebsiteDataTypeDiskCache,WKWebsiteDataTypeMemoryCache,WKWebsiteDataTypeLocalStorage,WKWebsiteDataTypeSessionStorage,WKWebsiteDataTypeOfflineWebApplicationCache], modifiedSince: dateFrom) {
//            
//        }
//        
        
        
        

        // Do any additional setup after loading the view.
    }
    func getTabbarVC() -> UITabBarController {
        let tabBarController = UITabBarController()
        
        let item1 = UITabBarItem(title: "书推", image: UIImage(named: "book0")?.imageWithRenderingMode(.AlwaysOriginal), selectedImage: UIImage(named: "book")?.imageWithRenderingMode(.AlwaysOriginal))
        let item2 = UITabBarItem(title: "电子书", image: UIImage(named: "learn0")?.imageWithRenderingMode(.AlwaysOriginal), selectedImage: UIImage(named: "learn")?.imageWithRenderingMode(.AlwaysOriginal))
        let item3 = UITabBarItem(title: "影视", image: UIImage(named: "mark0")?.imageWithRenderingMode(.AlwaysOriginal), selectedImage: UIImage(named: "mark")?.imageWithRenderingMode(.AlwaysOriginal))
        let item4 = UITabBarItem(title: "更多", image: UIImage(named: "more0")?.imageWithRenderingMode(.AlwaysOriginal), selectedImage: UIImage(named: "more")?.imageWithRenderingMode(.AlwaysOriginal))
        
        //navigationcontroller没法再pushnavigationcontroller
        let SB = UIStoryboard.init(name: "Main", bundle: NSBundle.mainBundle())
        
        let recVC = SB.instantiateViewControllerWithIdentifier("recommendViewController")
        
        let recommendViewController = UINavigationController(rootViewController: recVC)
        
        let ebookViewController = UINavigationController(rootViewController: EbookViewController())
        
        
        let markViewController = UINavigationController(rootViewController: MarkViewController())
        
        let moreViewController = UINavigationController(rootViewController: MoreViewController())
        
        recommendViewController.tabBarItem = item1
        ebookViewController.tabBarItem = item2
        markViewController.tabBarItem = item3
        moreViewController.tabBarItem = item4
        
        tabBarController.viewControllers = [recommendViewController,ebookViewController,markViewController,moreViewController]
        
        tabBarController.tabBar.tintColor = MAIN_COLOR
        
        return tabBarController
        

    }
    func responesToPhone(hidden : Bool){
        
        noticeSuccess("成功", autoClear: true, autoClearTime: 1)
        
        self.presentViewController(getTabbarVC(), animated: false) {
            print("成功")
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    func userContentController(userContentController: WKUserContentController, didReceiveScriptMessage message: WKScriptMessage) {
        NSLog("方法名:\(message.name) 传回:\(message.body)")
        
        self.hidden = message.body as? Bool
        
        self.responesToPhone(hidden!)
    }
    func webViewDidStartLoad(webView: UIWebView) {
        NSLog("启动")
    }
    
    func webViewDidFinishLoad(webView: UIWebView) {
        NSLog("结束")
    }
    
    func webView(webView: UIWebView, didFailLoadWithError error: NSError?) {
        NSLog("error:\(error)")
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
